package cn.itcast.web.controller.cargo;

import cn.itcast.common.utils.DownloadUtil;
import cn.itcast.domain.cargo.ContractProductVo;

import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jnlp.DownloadService;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class OutProductController extends BaseController{

    @Reference
    private ContractService contractService;

    //跳转出货表页面
    @RequestMapping("/print")
    public String print(){
        return "/cargo/print/contract-print";
    }

    /**
     *  模版打印
     */
    @RequestMapping("/printExcelTemplate")
    public void printExcelTemplate(String inputDate) throws IOException {
        //1.查询数据库获取数据列表
        List<ContractProductVo> list = contractService.findByShipTime(inputDate);
        //2.通过模板创建Excel文件
        //2.1 获取模板所在的路径
        String path = session.getServletContext().getRealPath("/")+"/make/xlsprint/tOUTPRODUCT.xlsx";
        //2.2 根据模板创建Excel的工作簿
        Workbook wb = new XSSFWorkbook(path);
        //2.3 获取第一页
        Sheet sheet = wb.getSheetAt(0);
        //2.4 处理大标题
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(1);
        cell.setCellValue(
                inputDate.replaceAll("-0","-").
                        replaceAll("-","年")+"月份出货表");
        //2.5 处理小标题(不需要处理)

        //2.6 设置数据行
        //提取模板中的样式
        row = sheet.getRow(2);
        CellStyle [] css = new CellStyle[9];  // [null,客户的样式，订单号的样式......]
        for (int i = 1; i < css.length; i++) {
            Cell cell1 = row.getCell(i);
            css [i] = cell1.getCellStyle(); //获取当前单元格的样式
        }

        int rowIndex = 2;
        for (ContractProductVo cp : list) {
            row = sheet.createRow(rowIndex);
            //客户
            cell = row.createCell(1);
            cell.setCellValue(cp.getCustomName());
            cell.setCellStyle(css[1]);
            //订单号
            cell = row.createCell(2);
            cell.setCellValue(cp.getContractNo());
            cell.setCellStyle(css[2]);
            // 货号
            cell = row.createCell(3);
            cell.setCellValue(cp.getProductNo());
            cell.setCellStyle(css[3]);
            // 数量
            cell = row.createCell(4);
            cell.setCellValue(cp.getCnumber());
            cell.setCellStyle(css[4]);
            // 工厂
            cell = row.createCell(5);
            cell.setCellValue(cp.getFactoryName());
            cell.setCellStyle(css[5]);
            // 工厂交期
            cell = row.createCell(6);
            cell.setCellValue(cp.getDeliveryPeriod());
            cell.setCellStyle(css[6]);
            // 船期
            cell = row.createCell(7);
            cell.setCellValue(cp.getShipTime());
            cell.setCellStyle(css[7]);
            // 贸易条款
            cell = row.createCell(8);
            cell.setCellValue(cp.getTradeTerms());
            cell.setCellStyle(css[8]);
            rowIndex ++;
        }

        //3.下载
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //将excel中数据写入到输出流bos
        wb.write(bos);
        new DownloadUtil().download(bos,response,"出货表.xlsx");
    }













    /**
     * 出货表打印
     *      1.查询数据库获取数据列表
     *      2.通过POI生成Excel
     *      3.下载Excel
     *      参数： inputDate(船期：查询条件)
     *      方法的返回值：不需要返回值，直接通过response的输出流下载文件即可
     */

    @RequestMapping("/printExcel")
    public void printExcel(String inputDate) throws IOException {
        //1.查询数据库获取数据列表
        List<ContractProductVo> list = contractService.findByShipTime(inputDate);
        //2.通过POI生成Excel
        //2.1 创建工作簿
        //Workbook wb = new XSSFWorkbook();//普通打印
        Workbook wb = new SXSSFWorkbook();//百万数据
        // 2.2 创建第一页sheet
        Sheet sheet = wb.createSheet("list");
        //设置单元格的宽度
        sheet.setColumnWidth(1,26 * 256); //第一个 列索引，宽度
        sheet.setColumnWidth(2,12 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(3,30 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(4,12 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(5,15 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(6,10 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(7,10 * 256); //第一个列索引，宽度
        sheet.setColumnWidth(8,8 * 256); //第一个列索引，宽度
        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));

        //2.3 创建大标题
        //2.3.1 创建第一行
        Row row = sheet.createRow(0);
        row.setHeightInPoints(36); //设置行高
        //2.3.2 创建第二个单元格
        Cell cell = row.createCell(1);
        cell.setCellStyle(bigTitle(wb));
        //2.3.3 设置数据内容  //2012年8月份出货表   --2019-01 --> 2019-1 -->2019年1 -->2019年1月份出货表
        //2019-01 --> 将-0 替换为 - 2019-1
        //2019-12 -->            - 2019-12
        cell.setCellValue(
                inputDate.replaceAll("-0","-").
                        replaceAll("-","年")+"月份出货表");
        //2.4 创建小标题
        row = sheet.createRow(1);
        row.setHeightInPoints((short)26.25); //设置行高
        String [] titles = new String[]{"","客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
        for (int i = 0; i < titles.length ; i++) {
            cell = row.createCell(i);
//			//设置单元格样式
//			CellStyle cs = wb.createCellStyle();
//			cs.setBorderTop(BorderStyle.THIN); //上边框细线
//			cs.setBorderBottom(BorderStyle.THIN); //上边框细线
//			cs.setBorderRight(BorderStyle.THIN);
//			cs.setBorderLeft(BorderStyle.THIN);
//			cs.setAlignment(HorizontalAlignment.CENTER);
//			cs.setVerticalAlignment(VerticalAlignment.CENTER);
//			Font font = wb.createFont();// 字体对象
//			font.setFontName("黑体");
//			font.setFontHeightInPoints((short)12);
//			cs.setFont(font);
//			cell.setCellStyle(cs);
            cell.setCellStyle(title(wb));

            cell.setCellValue(titles[i]);
        }



        //2.5 循环数据列表，构造数据行
        int rowIndex = 2;
        for (ContractProductVo cp : list) {
            row = sheet.createRow(rowIndex);
            row.setHeightInPoints(24); //设置行高
            //创建每个单元格，设置数据
            //客户
            cell = row.createCell(1);
            cell.setCellValue(cp.getCustomName());
            //cell.setCellStyle(text(wb));
            //订单号
            cell = row.createCell(2);
            cell.setCellValue(cp.getContractNo());
            //cell.setCellStyle(text(wb));
            // 货号
            cell = row.createCell(3);
            cell.setCellValue(cp.getProductNo());
            //cell.setCellStyle(text(wb));
            // 数量
            cell = row.createCell(4);
            cell.setCellValue(cp.getCnumber());
            //cell.setCellStyle(text(wb));
            // 工厂
            cell = row.createCell(5);
            cell.setCellValue(cp.getFactoryName());
            //cell.setCellStyle(text(wb));
            // 工厂交期
            cell = row.createCell(6);
            cell.setCellValue(cp.getDeliveryPeriod());
            //cell.setCellStyle(text(wb));
            // 船期
            cell = row.createCell(7);
            cell.setCellValue(cp.getShipTime());
            //cell.setCellStyle(text(wb));
            // 贸易条款
            cell = row.createCell(8);
            cell.setCellValue(cp.getTradeTerms());
            //cell.setCellStyle(text(wb));
            rowIndex++;
        }
        //3.下载Excel
        //ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //将excel中数据写入到输出流bos
        wb.write(bos);
        new DownloadUtil().download(bos,response,"出货表.xlsx");
    }

    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }

}
